package com.hzj.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzj.pojo.VoteItem;
import com.hzj.pojo.VoteOption;
import com.hzj.pojo.VoteSubject;
import com.hzj.service.VoteOptionService;
import com.hzj.service.VoteSubjectService;
import com.hzj.utils.OrderUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hzj
 * @since 2021-03-19
 */
@Controller

public class VoteSubjectController {
    @Autowired
    VoteSubjectService vss;
    @Autowired
    VoteOptionService vos;
    //判断是否是维护页面
    private int updateKeys = 0;

    @GetMapping("/admin/updateAll")
    public String adminUpdateAll(Integer updateKey) {
//        System.out.println();
        return "redirect:/showList?updateKey=" + updateKey;
    }

    @RequestMapping("/showList")
    public String showList(Long pageIndex, Long paheSize, String title, Model model, Integer updateKey) {
        if (updateKey != null) {
            updateKeys = updateKey;
        }

        model.addAttribute("updateKey", updateKeys);
        if (pageIndex == null || pageIndex == 0) {
            pageIndex = Long.valueOf(1);
        }
        if (paheSize == null || paheSize == 0) {
            paheSize = Long.valueOf(5);
        }

        Page page = new Page(pageIndex, paheSize);
        IPage<Map<String, Object>> mapIPage = vss.showCount(page, title);
//        System.out.println("当前：" + mapIPage.getCurrent() + "|" + mapIPage.getTotal());
//        System.out.println(mapIPage.getRecords());
        model.addAttribute("mapIPage", mapIPage);
        return "index";
    }


    @PostMapping("/admin/addSubject")
    public String addSubject(VoteSubject voteSubject, String[] options, Model model) {
        //生成随机id
        long random = OrderUtils.getRandom(8);
        voteSubject.setVsId(random);
//        System.out.println("voteSubject对象=====>" + voteSubject);
        List<VoteOption> optionList = new ArrayList<>();
        //选项存入集合
        for (int i = 0; i < options.length; i++) {
            String option = options[i];
//            System.out.println("投票选项=====>" + option);
            VoteOption voteOption = new VoteOption();
            voteOption.setVsId(random);
            voteOption.setVoOption(option);
            voteOption.setVoOrder(i + 1);
            optionList.add(voteOption);
        }
//        System.out.println("集合========>" + optionList);
        boolean b = vss.addSubjectAndOption(voteSubject, optionList);
        if (!b) {
            model.addAttribute("addError", "插入数据出错，请重试");
            return "add";
        }
        return "redirect:/showList";
    }

    @GetMapping("/checkSubject")
    public String checkSubject(int id, Model model) {
        Map<String, Object> stringObjectMap = vss.checkSubject(id);
        model.addAttribute("stringObjectMap", stringObjectMap);
//        System.out.println(stringObjectMap);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("vs_id", id);
        wrapper.orderBy(true, true, "vo_order");
        List list = vos.list(wrapper);
//        System.out.println(list);
        model.addAttribute("voteOptionList", list);
        return "vote";
    }


}


/**
 * if (pageIndex == null) {
 * pageIndex = 1;
 * <p>
 * }
 * if (paheSize == null) {
 * paheSize = 5;
 * }
 * System.out.println(pageIndex+"|"+paheSize+"|"+title);
 * Page page = vss.showAll(pageIndex, paheSize, title);
 * //        System.out.println(voteSubjects);
 * model.addAttribute("voteSubjectsList", page);
 */